<?php
namespace app\admin\controller;


use think\captcha\Captcha;
use think\Controller;
use think\Cookie;
use think\Db;
use think\Request;
use think\Session;

class Index extends Controller
{
    public function index(){//进入后台是否有登录的方法
        if(Cookie::has('admin')){
            $this->assign('admin',Cookie::get('admin'));
            return view("index");
        }else{
            return view("login");
        }
    }
//    public function index(){//进入后台是否有登录的方法
////        if(Cookie::has('admin')){
////            $this->assign('admin',Cookie::get('admin'));
////            return view("index");
////        }else{
////            return view("login");
////        }
//        return view("book");
//    }

    public function login(){//后台登录的
        if(Request::instance()->isPost()){
            $sql= new Captcha();
            if($sql->check($_POST['yzm'])){
                if($a=Db::table('admin')->where([
                    'username' => $_POST['username'],
                    'password' => $_POST['password']
                ])->find()){
                    Cookie::set('admin',$a['username'],360000);
                    Cookie::set('power',$a['power'],360000);
                    $this->success('登陆成功','Index/index1');
                }else{
                    $this->error('登录失败！');
                }
            }else{
                $this->error('验证码输入错误！');
            }
        }else{
            $this->error('数据传入有误！');
        }
    }

    public function index1(){//显示后台的index页面

            $this->assign('admin',Cookie::get('admin'));
            return view("index");

    }


    public function read(){//显示后台的

       return view("read");

    }

    public function zhuxiao(){//注销cookie , sessess
        Cookie::delete('admin');
        Cookie::delete('power');
        return view("login");

    }



}